package com.gzbugu.bugusys.action;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.fields.DatabaseFields;
import com.gzbugu.bugusys.service.IAddContractServiceService;
import com.gzbugu.bugusys.vo.ContractService;
import com.gzbugu.bugusys.vo.Staff;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Bokchoy 添加劳务合同记录
 */
@Controller
@Transactional
public class AddContractServiceAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String staffId;
	private Date contractServiceStart;
	private Date contractServiceEnd;
	private Date contractServiceUrl;
	private Integer contractServiceTypeId;
	private Date contractServiceDate;
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	@Resource(name = "addContractServiceService")
	private IAddContractServiceService service;
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "databaseFields")
	DatabaseFields databaseFields;

	public String execute() {
		ContractService instance = new ContractService();
		instance.setStaff((Staff) baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF));
		instance.setDate(contractServiceDate);
		instance.setEnd(contractServiceEnd);
		instance.setStart(contractServiceStart);
		instance.setDate(new Date(System.currentTimeMillis()));
		switch (contractServiceTypeId) {
		case 1:
			instance.setType(databaseFields.getCONTRACT_SERVICE_TYPE_INTERN());
			break;
		case 2:
			instance.setType(databaseFields.getCONTRACT_SERVICE_TYPE_TRIAL());
			break;
		case 3:
			instance.setType(databaseFields.getCONTRACT_SERVICE_TYPE_FORMAL());
			break;
		}
		String fileType = uploadFileName.substring(uploadFileName
				.lastIndexOf("."));
		service.save(instance, upload, fileType);
		return SUCCESS;
	}

	public void validate() {
		if (upload == null) {
			this.addFieldError("uploaderror", "please upload a file");
		}
		if (staffId == null) {
			this.addFieldError("staffIderror", "staffId can't be null");
		} else if (staffId.equals("")) {
			this.addFieldError("staffIderror", "staffId can't be null");
		} else if (baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF) == null) {
			this.addFieldError("staffIderror", "staff doesn't exists");
		}
		if (contractServiceTypeId == null) {
			this.addFieldError("contractServiceTypeIderror",
					"contractServiceTypeId can't be null");
		}
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Date getContractServiceStart() {
		return contractServiceStart;
	}

	public void setContractServiceStart(Date contractServiceStart) {
		this.contractServiceStart = contractServiceStart;
	}

	public Date getContractServiceEnd() {
		return contractServiceEnd;
	}

	public void setContractServiceEnd(Date contractServiceEnd) {
		this.contractServiceEnd = contractServiceEnd;
	}

	public Date getContractServiceUrl() {
		return contractServiceUrl;
	}

	public void setContractServiceUrl(Date contractServiceUrl) {
		this.contractServiceUrl = contractServiceUrl;
	}

	public Integer getContractServiceTypeId() {
		return contractServiceTypeId;
	}

	public void setContractServiceTypeId(Integer contractServiceTypeId) {
		this.contractServiceTypeId = contractServiceTypeId;
	}

	public Date getContractServiceDate() {
		return contractServiceDate;
	}

	public void setContractServiceDate(Date contractServiceDate) {
		this.contractServiceDate = contractServiceDate;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}
